﻿/**
 * @file FITKNastranAdaptorPart.h
 * @author zhourp (zhourunping@diso.cn)
 * @brief the class of adaptor for mesh part
 * @date 2024-11-14
 */
#ifndef FITKNASTRANADAPTORPART_H
#define FITKNASTRANADAPTORPART_H

#include "FITKNastranAbstractAdaptor.h"

#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKSectionAssign.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterial.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialBehavior.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialDamping.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialDensity.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialElastic.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialExpansion.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMeshPartVTK.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionBeam.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionShellHomogeneous.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"

#include <QQueue>
#include <QString>

namespace Nastran
{
/**
 * @brief the class of adaptor for mesh part
 * 
 * @author: zhourp (zhourunping@diso.cn)
 * @date: 2024-11-14
 */
class FITKNastranAdaptorPart : public FITKNastranAbstractAdaptor
{
  public:
    explicit FITKNastranAdaptorPart() = default;
    virtual ~FITKNastranAdaptorPart();

    /**
     * @brief 适配器读取
     * @return 状态 true成功， false失败
     * @author zhourp (zhourunping@diso.cn)
     * @date 2024-03-15
     */
    bool adaptR() override;

    /**
     * @brief 适配器写入
     * @return 状态 true成功， false失败
     * @author shenzhuowei (2931275006@qq.com)
     * @date 2025-03-04
     */
    bool adaptW() override;

    /**
     * @brief Assigns case data to the FITK Nastran Adaptor Part
     *
     * This method is responsible for setting the case data for the FITK Nastran Adaptor Part.
     *
     * @param[in] caseData Pointer to the AbaqusData::FITKDataCase object
     * @author zhourp(zhourunping@diso.cn)
     * @date 2024-11-14
     */
    void setCaseData(AbaqusData::FITKDataCase *caseData);

    /**
     * @brief Assigns the mesh part to the Nastran adaptor.
     *
     * This function is used to assign the mesh part from Abaqus data to the Nastran adaptor.
     *
     * @param[in] part Pointer to the Abaqus mesh part.
     * @author zhourp(zhourunping@diso.cn)
     * @date 2024-11-14
     */
    void setMeshPart(AbaqusData::FITKAbaqusPart *part);

    /**
     * @brief Sets the file content cache for the Nastran adaptor part.
     *
     * This function updates the internal file content cache of the Nastran adaptor part with the provided cache data.
     *
     * @param[in] cache Queue containing the file content cache data.
     * @author zhourp(zhourunping@diso.cn)
     * @date 2024-11-14
     */
    void setFileContentCache(QQueue<QString> cache);

    /**
    * @brief 获取所有需写入文件的队列
    * @author shenzhuowei (2931275006@qq.com)
    * @date 2025-03-05
    */
    void getFileContentCache(QQueue<QString>& cache);

  private:
    /**
     * @brief Reads GRID data from the file content cache.
     *
     * This function processes GRID data from the file content cache, extracting node information and adding it to the
     * mesh.
     *
     * @return True if the GRID data is read and processed successfully, false otherwise.
     * @author zhourp(zhourunping@diso.cn)
     * @date 2024-11-14
     */
    bool readGRID();
    /**
    * @brief 读取短格式三维坐标
    * @param[o] coor 输出节点坐标
    * @param[i] text 文本信息
    * @author LiBaojun (libaojunqd@foxmail.com)
    * @date 2024-03-08
    */
    bool readCoordinateShort(double* coor, const QString & text);

    /**
     * @brief Reads CTRIA3 data from the file content cache.
     *
     * This function processes CTRIA3 data from the file content cache, extracting element information and adding it to
     * the mesh.
     *
     * @return True if the CTRIA3 data is read and processed successfully, false otherwise.
     * @author zhourp(zhourunping@diso.cn)
     * @date 2024-11-14
     */
    bool readCTRIA3();

    /**
     * @brief Reads CQUAD4 data from a file and processes it.
     *
     * This function reads data related to CQUAD4 elements from a file. It checks for
     * necessary data and structures, then processes the data line by line, extracting
     * element IDs, property IDs, and node lists. It then adds the elements to the mesh
     * and updates cell group information.
     *
     * @return True if the reading and processing are successful, false otherwise.
     *
     * @author zhourp(zhourunping@diso.cn)
     * @date 2024-11-14
     */
    bool readCQUAD4();

    /**
     * @brief Reads CHEXA data from a file and processes it.
     * @return True if the data is read and processed successfully, false otherwise.
     *
     * This function reads CHEXA data from a file and processes it. It checks for various conditions
     * like the availability of meshPart, caseData, reader, helper, and fileContentCache. If any of these
     * conditions are not met, the function returns false. Otherwise, it reads the CHEXA data, processes it,
     * and returns true.
     *
     * @author zhourp(zhourunping@diso.cn)
     * @date 2024-11-14
     */
    bool readCHEXA();

    /**
     * @brief Reads CTETRA data from a file and updates the mesh if necessary data structures are initialized.
     * @return True if the data is read and processed successfully, false otherwise.
     *
     * This function checks if the required data structures for reading CTETRA data are initialized.
     * It then reads the data from the file, extracts element and property IDs, and node IDs.
     * If the mesh is initialized, it appends the element to the mesh based on the number of nodes.
     * Finally, it inserts cells group information.
     *
     * @author zhourp(zhourunping@diso.cn)
     * @date 2024-11-14
     */
    bool readCTETRA();

private:
    /**
    * @brief 写入节点信息
    * @author shenzhuowei (2931275006@qq.com)
    * @date 2025-03-05
    */
    bool writeGRID();

    /**
    * @brief 填充Section和Elem的映射表
    * @author shenzhuowei (2931275006@qq.com)
    * @date 2025-03-05
    */
    void initSectionElemMap();
    
    /**
    * @brief 写入TRIA3类型单元信息
    * @author shenzhuowei (2931275006@qq.com)
    * @date 2025-03-05
    */
    bool writeCTRIA3();

    /**
    * @brief 写入QUAD4类型单元信息
    * @author shenzhuowei (2931275006@qq.com)
    * @date 2025-03-05
    */
    bool writeCQUAD4();

private:

    /**
     * @brief 算例数据
     * @author zhourp (zhourunping@diso.cn)
     * @date 2024-03-15
     */
    AbaqusData::FITKDataCase *_caseData{};

    /**
     * @brief mesh data pointer
     * @author zhourp (zhourunping@diso.cn)
     * @date 2024-03-15
     */
    AbaqusData::FITKAbaqusPart *_meshPart{};

    /**
     * @brief file window cache
     * @author zhourp (zhourunping@diso.cn)
     * @date 2024-03-15
     */
    QQueue<QString> _fileContentCache{};

    /**
    * @brief Section和Elem的映射表
    * @author shenzhuowei (2931275006@qq.com)
    * @date 2025-03-05
    */
    QMap<int, int> _section2ElemMap{};
};

Register2FITKIOAdaptorFactory(BDF, AbaqusData::FITKAbaqusPart, FITKNastranAdaptorPart)
} // namespace Nastran

#endif // FITKNASTRANADAPTORPART_H
